<?php
/**
 * 跃飞科技版权所有 @2017
 * User: 钟贵廷
 * Date: 2017/5/24
 * Time: 11:13
 */
namespace app\admin\model;
use think\Image;
use think\Model;

class Ad extends Model
{
    /**
     * 搜索
     * @return array
     */
    public function search($keyword)
    {
        // 搜索所有的数据,如果需要搜索其他字段需要自己添加
        $where = 1;
        $where .= " and (ad_name like '%$keyword%')";
        /** 翻页 **********/
        //1 . 算出总的记录数
        $count = $this->where($where)->count();
        // 2. 生成翻页类的对象
        $page = new \Component\Page($count,config('PAGE_SIZE'),input('page'));
        $page->config['header'] = '个广告';
        // 3. 生成翻页的字符串：上一页、下一页
        $pageStr = $page->fpage();
        // 4. 取出当前页的数据
        $data = $this->where($where)->limit( $page->limit)->order("id desc")->select();
        return array(
            'page' => $pageStr,
            'data' => $data,
        );
    }

    /**
     * 上传图片
     */
    public function upload($adInfo)
    {
        $data = [];
        // 如果上传了图片，并且图片是在临时目录中的（说明是新上传的）
        if($adInfo['ad_img'] && strpos($adInfo['ad_img'], 'tmp') !== FALSE)
        {
            if(isset($adInfo['ad_size']) && !empty($adInfo['ad_size']))
            {
                if(isset($adInfo['id']))
                {
                    // 修改就删除原图
                    if(isset($adInfo['old_ad_img']))
                    {
                        foreach ($adInfo['old_ad_img'] as $v)
                        {
                            $v = ROOT_PATH.'public'.$v;
                            if(is_file($v))
                            {
                                unlink($v);
                            }
                        }
                    }
                }

                list($width,$height) = explode(',',$adInfo['ad_size']);
                //优化代码
                $rootPath = ROOT_PATH.'public/';
                // 构造图片存放目录的路径
                $date = date('Y-m',time());
                //目录结构宽高/年月组成
                $dir =$rootPath."/assets/upload/ad/{$width}x{$height}/$date";
                if(!is_dir($dir))
                {
                    mkdir($dir, 0777,true);
                }
                $adName = time().'.'.substr(strrchr($adInfo['ad_img'], '.'), 1);
                $imgName = $dir.'/'. $adName; //原图
                $sm1_logo = $dir.'/sm1_'. $adName;  //100
                $sm2_logo = $dir.'/sm2_'. $adName;  //真正的广告图
                copy($rootPath.$adInfo['ad_img'],$imgName);
                $image = Image::open($imgName);

                $image->thumb($width, $height,2)->save($sm2_logo);
//                $image->thumb( $height,$width)->save($sm2_logo);
                $image->thumb(100, 100*$height/$width,2)->save($sm1_logo);
//                $image->thumb( 100*$height/$width,100)->save($sm1_logo);

                $data['ad_img'] = substr($imgName,strlen($rootPath));
                $data['sm2_logo'] = substr($sm2_logo,strlen($rootPath));
                $data['sm1_logo'] = substr($sm1_logo,strlen($rootPath));
            }
        }
        return $data;
    }
}